﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace Icst.Leaflike
{
    public enum SearchOption
    {
        /// <summary>
        /// none
        /// </summary>
        None,

        /// <summary>
        /// int
        /// </summary>
        Shugao,
        /// <summary>
        /// string
        /// </summary>
        Shuxing,

        /// <summary>
        /// 排列方式选项
        /// </summary>
        Yexu,
        /// <summary>
        /// 单复叶
        /// </summary>
        Danfuye,

        /// <summary>
        /// leaf id
        /// </summary>
        Yexing,
        /// <summary>
        /// 叶尖选项
        /// </summary>
        Yejian,
        /// <summary>
        /// 叶柄选项
        /// </summary>
        Yeji,
        /// <summary>
        /// 页边选项
        /// </summary>
        Yeyuan,

        /// <summary>
        /// 花朵形态
        /// </summary>
        Huaxing,

        /// <summary>
        /// 花期
        /// </summary>
        Huaqi,

        /// <summary>
        /// 果实形态
        /// </summary>
        Guoshi,
        /// <summary>
        /// 果实颜色
        /// </summary>
        Guoshiyanse,

        /// <summary>
        /// 树皮形态
        /// </summary>
        Shupi,

        /// <summary>
        /// 树干颜色
        /// </summary>
        Shuganyanse,

        /// <summary>
        /// 是否采用的是上传模式
        /// </summary>
        isUpload,
    }

    public class SearchQuery : Dictionary<SearchOption, string> 
    {
        public bool IsUpload { get { return string.Equals(this[SearchOption.isUpload], "true", StringComparison.InvariantCultureIgnoreCase); } }
        public SearchQuery() 
        {
            Clear();
        }
        public void Clear()
        {
            foreach (var name in Enum.GetValues(typeof(SearchOption)))
            {
                this[(SearchOption)name] = string.Empty;
            }
        }
        private void AppendRow(StringBuilder sb, string name, SearchOption option, string vauleFormat)
        {
            const string ROW_FORMAT = "<tr><td>{0}</td><td>{1}</td></tr>";
            sb.AppendFormat(ROW_FORMAT, name, (string.IsNullOrEmpty(this[option]) || this[option] == "00") ? "未知" : string.Format(vauleFormat, this[option]));
            sb.AppendLine();
        }
        public string ToListString()
        {
            StringBuilder writer = new StringBuilder();
            writer.AppendLine("<table>");
            foreach (var option in this)
            {
                switch (option.Key)
                {
                    case SearchOption.Shugao: AppendRow(writer, "高度", option.Key, "{0}"); break;
                    case SearchOption.Shuxing: AppendRow(writer, "整体形态", option.Key, "{0}"); break;
                    case SearchOption.Yexu: AppendRow(writer, "树的枝叶", option.Key, "{0}"); break;
                    case SearchOption.Yexing: AppendRow(writer, "叶形", option.Key, "{0}"); break;
                    case SearchOption.Yejian: AppendRow(writer, "叶尖选项", option.Key, "{0}"); break;
                    case SearchOption.Yeji: AppendRow(writer, "叶柄选项", option.Key, "{0}"); break;
                    case SearchOption.Yeyuan: AppendRow(writer, "页边选项", option.Key, "{0}"); break;
                    case SearchOption.Huaxing: AppendRow(writer, "花朵形态(花期、颜色、花瓣数)", option.Key, "{0}"); break;
                    case SearchOption.Guoshi: AppendRow(writer, "果实形态", option.Key, "{0}"); break;
                    case SearchOption.Guoshiyanse: AppendRow(writer, "果实颜色", option.Key, "{0}"); break;
                    case SearchOption.Shupi: AppendRow(writer, "树皮、树干形态", option.Key, "{0}"); break;
                    default: break;
                }
            }
            writer.AppendLine("</table>");
            return writer.ToString();
        }
        public string[] ToArray()
        {
            List<string> values = new List<string>();
            foreach (var option in this)
            {
                if (!string.IsNullOrEmpty(option.Value))
                    values.Add(option.Value);
            }
            return values.ToArray();
        }
        public override string ToString()
        {
            List<string> options = new List<string>();
            foreach (var option in this)
            {
                if(option.Key != SearchOption.None)
                    options.Add(string.Format("{0}={1}", option.Key.ToString().ToLower(), option.Value));
            }
            string query = string.Join("&",options.ToArray());
            return query;
        }
        public static SearchQuery From(NameValueCollection parameters)
        {
            SearchQuery query = new SearchQuery();

            string mode = parameters["m"];
            string advancedOptions = parameters["adv"];
            if (!string.IsNullOrEmpty(advancedOptions))
            {
                string[] pairs = advancedOptions.Split(';');
                foreach (string pair in pairs)
                {
                    string[] keyvalue = pair.Split(':');
                    if (!string.IsNullOrEmpty(keyvalue[1]))
                        query[(SearchOption)Enum.Parse(typeof(SearchOption),keyvalue[1],false)] = keyvalue[1];
                }
            }
            return query;
        }
        public bool HasShuxing 
        { 
            get 
            {
                return OptionValid(SearchOption.Shugao)
                    || OptionValid(SearchOption.Shuxing); 
            }
        }
        public bool HasZhiye
        {
            get
            {
                return OptionValid(SearchOption.Yexu) ||
                    OptionValid(SearchOption.Danfuye);
            }
        }
        public bool HasYexi
        {
            get
            {
                return OptionValid(SearchOption.Yeji) ||
                    OptionValid(SearchOption.Yejian) ||
                    OptionValid(SearchOption.Yexing) ||
                    OptionValid(SearchOption.Yeyuan);
            }
        }
        public bool HasHuaxing
        {
            get
            {
                return OptionValid(SearchOption.Huaqi)
                    || OptionValid(SearchOption.Huaxing)
                    || OptionValid(SearchOption.Guoshi)
                    || OptionValid(SearchOption.Guoshiyanse);
            }
        }
        public bool HasShupi
        {
            get
            {
                return OptionValid(SearchOption.Shuganyanse)
                    || OptionValid(SearchOption.Shupi);
            }
        }

        private bool OptionValid(SearchOption option) 
        { 
            string value = this[option].Trim();
            return !string.IsNullOrEmpty(value) && value != "未知" && value != "00";
        }
    }
}
